// pages/workerCode/task.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    top: 0, //标题图标距离顶部距离
    opacity: 1,
    statusBarHeight:20,
    scrollTop: 0.5,
    sub:{cate:0,latitude:0,longitude:0,page:1},
    total:0,
    popupShow: false,
    popupItem:{title:'',user:{}},
    timeArea:{},
    list:[],
  },
  onShow:function(){
    this.setData({
      list:[]
    })
    let latitude=wx.getStorageSync("worker_latitude");
    let longitude=wx.getStorageSync("worker_longitude");
    if(!latitude || !longitude){
      this.getLocation();
    }else{
      this.load();
    }
  },
  onLoad: function(options) {
    let obj = {};
    obj = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let top = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
        let statusBarHeight=res.statusBarHeight;
        this.setData({
          top: top,
          statusBarHeight:statusBarHeight
        })
      }
    });
    this.loadTime();
  },
  toSearch(){
    wx.navigateTo({
      url:"/pages/workerCode/search?longitude="+this.data.sub.longitude+"&latitude="+this.data.sub.latitude
    })
  },
  async getLocation(){
    let ad=await wx.util.getLocation();
    this.setData({
      'sub.longitude':ad.longitude,
      'sub.latitude':ad.latitude
    })
    this.load();
  },
  async loadTime(){
    let res=await wx.util.post("task/get_time");
    this.setData({
      timeArea:res.data
    })
  },
  async load(){
    let res=await wx.util.post("task/index",this.data.sub,false);
    this.setData({
      list:res.data.data
    })
    if(this.data.sub.cate==0){
      this.setData({
        total:res.data.total
      })
    }
  },
  close(){
    this.setData({
      popupShow:false
    })
  },
  setCate(s){
    let e=s.currentTarget.dataset.index;
    this.setData({
      popupShow:false,
      'sub.page':1,
      'sub.cate':e,
      list:[]
    })
    this.load();
  },
  acPop(e){
    if(e.detail.type=="刷新"){
      this.setCate({currentTarget:{dataset:{index:0}}});
      return;
    }
  },
  btnclick(s){
    let e=s.detail;
    if(e.type=="more"){
      this.setData({
        popupShow:true,
        popupItem:e.data
      })
    }
  },
  initNavigation(e) {
    this.setData({
      top: e.detail.top,
      opacity: e.detail.opacity
    })
  },
  opacityChange(e) {
  },
})